{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9074ec29",
   "metadata": {},
   "source": [
    "------\n",
    "* 记录学习人：杨茜\n",
    "* 时间：2021-11-15\n",
    "* week12\n",
    "* 学习资源：智超的博客、笔记记录的内容"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4c00686",
   "metadata": {},
   "source": [
    "## 1.字典的一般方法\n",
    "\n",
    "> 1. 字典的查询 查询key ➡ value\n",
    ">> 1. 先看\n",
    ">> 2. 字典的修改\n",
    "> 3. 字典的删除\n",
    "> 4. 字典的增加(dict_name[key] = value)\n",
    "\n",
    "\n",
    "## 2.字典的进阶方法\n",
    "\n",
    ">1. 字典的初始化\n",
    ">>1. D.fromkeys()\n",
    ">>2. D.setdefault()\n",
    "\n",
    "## 3.字典的实践(一)\n",
    "\n",
    "## 3.字典的实践(二)\n",
    "\n",
    "## 3.字典的实践(三)课外"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ae2c60a",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_info = (\n",
    "    \"stu1801\":\"yangqian\"\n",
    "    \"stu1802\":\"YQ\"\n",
    "    \"stu1803\":\"Yang\"\n",
    ")\n",
    "print(stu_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f20c4c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 直接查找 dict_name[key]\n",
    "stu_info[\"stu1801\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "194f8af9",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_info[\"stu1804\"]\n",
    "\n",
    "# key的关键词报错的时候返回none"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "af34c59f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用get方法查找\n",
    "help(stu_info.get)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f57567fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_info.get(\"stu1804\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e27efbd",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(stu_info.get(\"stu1804\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de6f0a5b",
   "metadata": {},
   "source": [
    "> 2.修改 AND 新增\n",
    ">> 1.先查询 查看key是否存在 使用in查询成员"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9855d91a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查询 stu1804\n",
    "\"stu1804\" in stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74f16f0b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 如果Key存在 修改\"stu1803\":\"lihua\" 变为 \"stu1803\":\"Lin\"\n",
    "if \"stu1803\" in stu_info:\n",
    "    stu_info[\"stu1803\"] = \"Lin\"\n",
    "stu_info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f6aa517",
   "metadata": {},
   "source": [
    "> 3.字典的删除(.+tab显示所有方法)\n",
    ">> 1.D.pop()的一般操作如下：\n",
    ">>> 1. 先查询是否存在 in\n",
    ">>> 2. 再使用删除的方法 \n",
    ">> 2.D.pop item()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d0009766",
   "metadata": {},
   "outputs": [],
   "source": [
    "help(stu_info.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bb91cb59",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_info.pop() # 不正确 不属于列表 删除列表最后一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e826558",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典删除的正确使用\n",
    "if 'stu1803' in stu_info:\n",
    "    stu_info.pop('stu1803')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3280fd07",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a9f76ee4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 如果不先查询key是否存在，有可能报错： KeyError:'stu1804'\n",
    "stu_info.pop('stu1804')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ac50be2",
   "metadata": {},
   "outputs": [],
   "source": [
    "help(stu_info.popitem) # 随机删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aed96bb9",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_info.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e345cb8",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa188ab7",
   "metadata": {},
   "source": [
    "## 字典的实践(一)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7cba9a44",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个你要查询元音的单词word\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 0, 'i': 0, 'o': 1, 'u': 0}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 目标 查询 元音字母是否出现在word文档中，并且统计出现次数\n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"请输入一个你要查询元音的单词\")\n",
    "found = {}\n",
    "\n",
    "# 字典初始化的值\n",
    "# found[\"a\"]= 0\n",
    "# found[\"e\"]= 0\n",
    "# found[\"i\"]= 0\n",
    "# found[\"o\"]= 0\n",
    "# found[\"u\"]= 0\n",
    "\n",
    "for i in vowels:\n",
    "    # 使用字典的新增，构建初始化字典\n",
    "    found[i]= 0\n",
    "    \n",
    "# 循环用户输入的文本，出现一次元音字符，对应的元音字符+1\n",
    "for i in word :\n",
    "    if i in vowels:\n",
    "        found[i] += 1\n",
    "found\n",
    "\n",
    "# for k,v in sorted(found.items()):\n",
    "#     print(k,'was found',v,\"time(s).\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9ea7a69",
   "metadata": {},
   "source": [
    "> 字典的进阶方法：初始化所有值fromkeys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ea0cf08",
   "metadata": {},
   "outputs": [],
   "source": [
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "# 初始化 字典\n",
    "found = dound.fromkey(vowels,10)\n",
    "\n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        found[i] += 1\n",
    "found"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44b8885d",
   "metadata": {},
   "source": [
    "> 字典的进阶方法：初始化特定的值 setdefault()\n",
    "\n",
    ">> * 查询 key 是否出现在字典当中\n",
    ">>> 1. 出现过就当没看见，不作处理\n",
    ">>> 2. 从未出现过，给定 key 以及 value（指定的初始化的值）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8a9648d",
   "metadata": {},
   "outputs": [],
   "source": [
    "help(found.setdefalut)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74fc77c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_password = {\n",
    "    \"xiaoming\":\"222333\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b6560cb0",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_password.setdefault(\"lihua\",\"123456\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a69f8304",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_password.setdefault(\"xiaohong\",\"123456\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0cc23313",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_password.setdefault(\"xiaoming\",\"123456\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e2bde552",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_password"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb709a6b",
   "metadata": {},
   "source": [
    "## 字典的实践（二）\n",
    "\n",
    "* 课本P121"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3100a08",
   "metadata": {},
   "outputs": [],
   "source": [
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        found.setdefault(i,0)   # 特定情况下（字典中不存在该元素时，初始化其值）\n",
    "        found[i] += 1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38aecf91",
   "metadata": {},
   "outputs": [],
   "source": [
    "{'e':1,'o':1,'i':1,'a':1}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "334bd177",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
